package com.disdr.picture;

import com.disdr.utils.印章抠图;
import com.disdr.utils.签名抠图;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;

public class 收据填充 {


    static String name = "阿心航空";//诚德冷链
    static String saveBase = "F:\\桌面\\回收站\\阿心航空0106\\";

    public static void main(String[] args) throws Exception {

        String 石头人 = "449157744,2022/12/20,湘A6A686,百货,10;449164693,2022/12/21,湘A6A686,百货,10;449177586,2022/12/22,湘A6A686,百货,10;449184000,2022/12/23,湘A6A686,百货,10;449196541,2022/12/24,湘A6A686,百货,10;449203912,2022/12/25,湘A6A686,百货,10;449215377,2022/12/26,湘A6A686,百货,10;449223684,2022/12/27,湘A6A686,百货,10;449237292,2022/12/28,湘A6A686,百货,10;449245462,2022/12/29,湘A6A686,百货,10;449254561,2022/12/30,湘A6A686,百货,10;449265188,2022/12/31,湘A6A686,百货,10;449156228,2022/12/20,湘A6A686,百货,10;449167207,2022/12/21,湘A6A686,百货,10;449175695,2022/12/22,湘A6A686,百货,10;449187435,2022/12/23,湘A6A686,百货,10;449195175,2022/12/24,湘A6A686,百货,10;449206270,2022/12/25,湘A6A686,百货,10;449215501,2022/12/26,湘A6A686,百货,10;449225549,2022/12/27,湘A6A686,百货,10;449234599,2022/12/28,湘A6A686,百货,10;449243464,2022/12/29,湘A6A686,百货,10;449253246,2022/12/30,湘A6A686,百货,10;449263939,2022/12/31,湘A6A686,百货,10;449154227,2022/12/20,湘AA8186,百货,10;449167430,2022/12/21,湘AA8186,百货,10;449176702,2022/12/22,湘AA8186,百货,10;449185307,2022/12/23,湘AA8186,百货,10;449197055,2022/12/24,湘AA8186,百货,10;449206106,2022/12/25,湘AA8186,百货,10;449216440,2022/12/26,湘AA8186,百货,10;449223324,2022/12/27,湘AA8186,百货,10;449235762,2022/12/28,湘AA8186,百货,10;449243381,2022/12/29,湘AA8186,百货,10;449257075,2022/12/30,湘AA8186,百货,10;449263418,2022/12/31,湘AA8186,百货,10;449157432,2022/12/20,湘AB2136,百货,10;449164456,2022/12/21,湘AB2136,百货,10;449175238,2022/12/22,湘AB2136,百货,10;449184524,2022/12/23,湘AB2136,百货,10;449195205,2022/12/24,湘AB2136,百货,10;449205266,2022/12/25,湘AB2136,百货,10;449217589,2022/12/26,湘AB2136,百货,10;449226577,2022/12/27,湘AB2136,百货,10;449235986,2022/12/28,湘AB2136,百货,10;449243440,2022/12/29,湘AB2136,百货,10;449254791,2022/12/30,湘AB2136,百货,10;449267388,2022/12/31,湘AB2136,百货,10";
        for(String str : 石头人.split(";")){

        /*FileReader fileReader = new FileReader("收据填充数据\\回单数据.txt");
        BufferedReader br = new BufferedReader(fileReader);
        String line = br.readLine();
        for(;line!=null;line = br.readLine()){*/
            System.out.println(str);
            generateReceipt(str.split(","));
        }
    }
    /**
     * 环运回单
     *
     * @return
     * @throws IOException
     */
    public static BufferedImage generateReceipt(String[] obj) throws Exception {

        File file = new File("src\\main\\resources\\templates\\收据.jpg");
        BufferedImage bi = ImageIO.read(file);
        //System.out.println(bi.getWidth());//1706
        //System.out.println(bi.getHeight());//1280

        // 得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(Color.GRAY);//设置字体颜色
        Font font2 = new Font("", 0, 25);
        g2.setFont(font2);
        /*Date date = DateUtil.parse(obj[1]);
        g2.drawString(DateUtil.format(date,"yyyy"), 700, 440);
        g2.drawString(DateUtil.format(date,"M"), 810, 440);
        g2.drawString(DateUtil.format(date,"d"), 890, 440);*/

        Font font = new Font("", 0, 35);
        g2.setFont(font);

        /*for(int y =20;y<1280;y+=30){
            g2.drawString(String.valueOf(y), 800, y);
        }
        int a = 1;
        for(int x =20;x<1706;x+=50){
            if(a==1){
                a=-1;
                g2.drawString(String.valueOf(x), x,600-20);
            }else{
                a=1;
                g2.drawString(String.valueOf(x), x,600+20);
            }
        }*/

        g2.drawString(name, 370, 420);

        g2.drawString(obj[3], 400, 590);
        g2.drawString(obj[4], 720, 590);

        g2.drawString(obj[2], 920, 590);
        //g2.drawString("许盛辉", 1410, 995);

        g2.setColor(new Color(178,97,104));// 设置字体颜色
        g2.drawString(obj[0], 1250, 382);//红色编号

        //手写签名
        签名抠图.签名抠图(bi,(int)(Math.random()*20)+1410,(int)(Math.random()*20)+990,60,ImageIO.read(new File("src\\main\\resources\\templates\\林杰.jpg")));
        //公司盖章
        印章抠图.印章抠图(bi,(int)(Math.random()*30)+135,(int)(Math.random()*20)+735,1.0,name);
        //印章抠图.印章抠图(bi,(int)(Math.random()*10)+145,(int)(Math.random()*30)+780,1.0,ImageIO.read(new File("src\\main\\resources\\templates\\印章\\晓年物流.jpg")));
        //印章抠图.印章抠图(bi,(int)(Math.random()*10)+145,(int)(Math.random()*30)+780,1.0,ImageIO.read(new File("src\\main\\resources\\templates\\印章\\桓垣物流.png")));
        //印章抠图.印章抠图(bi,(int)(Math.random()*10)+145,740,1.0,ImageIO.read(new File("D:\\user\\Desktop\\1666320186574.png")));

        g2.dispose();//释放对象

        //随机尺寸裁剪，以假乱真(0_0)
        int x = (int)(Math.random()*50)+120;
        int y = (int)(Math.random()*260);
        int xx = (int)(Math.random()*136)+1570;
        int yy = (int)(Math.random()*190)+1090;

        String name = obj[2]+"_"+obj[1].replaceAll("\\/","-").substring(0,10);
        String nn = name;
        int i = 1;
        while (过磅单填充.nameSet.contains(nn)){
            nn = name+"_"+i;
            i++;
        }
        过磅单填充.nameSet.add(nn);
        nn = nn +".jpg";
        //String nn = obj[0]+".jpg";

        ImageIO.write(bi.getSubimage(x,y,xx-x,yy-y), "jpg", new FileOutputStream(saveBase+nn));//保存图片 JPEG表示保存格式
        return bi;

    }
}
